package com.xuwang.controller;

import com.xuwang.pojo.Additive;
import com.xuwang.service.impl.AdditiveServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 食品添加剂表(Additive)表控制层
 *
 * @author abaaba
 * @since 2021-09-13 18:35:48
 * @version 1.0
 */
@RestController
@RequestMapping("/additive")
@Api(value = "食品添加剂表(Additive)管理",tags = "食品添加剂表(Additive)管理接口API")
@ApiResponses({
        @ApiResponse(code=400,message="请求参数不完整或者错误"),
        @ApiResponse(code=404,message="找不到页面"),
        @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code=406,message="页面地址和接口名冲突"),
        @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code=500,message="后台服务逻辑错误")
})
public class AdditiveController {
    /**
     * 服务对象
     */
    @Resource
    private AdditiveServiceImpl additiveServiceImpl;

    @ApiOperation(value = "模板导入接口", notes = "模板导入接口")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException {
        additiveServiceImpl.downloadTemplate(request, response);
    }
    @Transactional
    @PostMapping("/importExcel")
    @ApiOperation(value = "添加剂批量导入接口", notes = "批量导入接口")
    public void importExcel(MultipartFile file, HttpServletRequest request) throws IOException {
        additiveServiceImpl.importExcel(file,request, "uploadExcel");
    }
    @Transactional
    @GetMapping("/exportExcel")
    @ApiOperation(value = "批量导出接口", notes = "批量导出接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id集合(格式例子：1,2,3)", paramType = "query", dataType = "String", required = true),
    })
    public void exportExcel(@RequestParam String ids, HttpServletResponse response) throws IOException {
        additiveServiceImpl.exportExcel(ids, response);
    }
    /**
     * 查询本公司所有添加剂信息
     */
    @RequestMapping(value = "/selectAllTjjByToken", method = RequestMethod.GET)
    @ApiOperation(value = "生产的时候，通过token查询本公司所有的添加剂（当查询供货是token为null）",notes = "生产的时候，通过token查询本公司所有的添加剂",httpMethod = "GET")
    public Map<String, Object> selectAllTjjByToken(@Autowired HttpServletRequest request){
//        int index=1;
        return this.additiveServiceImpl.selectAllTjjByToken(request);
    }

    /**
     * 分页模糊查询所有添加剂数据
     * @param additiveName      添加剂名
     * @param superCompanyName  供货商名
     * @param companyAddress    供货商地址
     * @param additiveWarranty  保质期
     * @return 分页模糊查询得到的添加剂数据
     */
    @RequestMapping(value = "/selectForPage", method = RequestMethod.GET)
    @ApiOperation(value = "列表查询分页接口",notes = "列表查询分页接口",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "page",value = "每页条数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "additiveName",value = "添加剂名称",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "superCompanyName",value = "供应商名称",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "companyAddress",value = "供应商地址",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "additiveWarranty",value = "添加剂保质期",paramType = "query",dataType = "Integer",required = false),
            @ApiImplicitParam(name = "isDelete",value = "是否已删除",paramType = "query",dataType = "String",required = false)
    })
    public Map<String, Object> selectForPage(@RequestParam(name = "index") int index, @RequestParam(name = "page") int page,String additiveName,String superCompanyName,String companyAddress,Integer additiveWarranty,String isDelete) {
//        int index=1;
        return this.additiveServiceImpl.selectForPage(index,page,additiveName,superCompanyName,companyAddress,additiveWarranty,isDelete);
    }
    /**
     * 通过一个pojo对象新增单条数据
     *
     * @param additive 一个数据库对应的POJO数据对象
     * @return 返回插入的主键id
     */
    @Transactional
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "根据完整记录新增一个**的接口",notes = "根据完整记录新增一个**的接口",httpMethod = "POST")
    public Map<String, Object> insert(@RequestBody @ApiParam(name = "additive",value = "vo模型",required = true)Additive additive, @Autowired HttpServletRequest request) {
        return this.additiveServiceImpl.insert(additive,request);
    }

    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
//     * @param isDelete 是否已删除
     * @return 单条数据
     */
    @RequestMapping(value = "/updateIsDeleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据一个主键修改一条删除状态的接口",notes = "根据一个主键修改一条删除状态的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",dataType = "String",required = true)
//            @ApiImplicitParam(name = "isDelete",value = "是否删除",paramType = "query",dataType = "int",required = true),
    })
    public Map<String, Object> updateIsDeleteById(@RequestParam("id") String id) {
        return this.additiveServiceImpl.updateIsDeleteById(id,1);
    }

    /**
     * 批量删除多条数据
     *
     * @param ids 批量主键
     * @return 单条数据
     */
    @Transactional
    @RequestMapping(value = "/updateIsDeleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除多条的接口",notes = "删除多条的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "主键",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> updateIsDeleteByIds(@RequestParam("ids") String ids) {
        return this.additiveServiceImpl.updateIsDeleteByIds(ids);
    }



    
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ApiOperation(value = "通过主键查询一个**的接口",notes = "通过主键查询一个**的接口",httpMethod = "GET")
    public Map<String, Object> selectById(@RequestParam("id") String id) {
        return this.additiveServiceImpl.selectById(id);
    }
    
    /**
     * 通过主键更新单条数据(传入所有信息除了sup)
     *
     * @param additive 一个数据库对应的POJO数据对象
     * @return 单条数据
     */
    @Transactional
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ApiOperation(value = "通过主键更新一个**的接口",notes = "通过主键更新一个**的接口",httpMethod = "POST")
    public Map<String, Object> updateById(@RequestBody @ApiParam(name = "additive",value = "pojo模型",required = true) Additive additive,
    @Autowired HttpServletRequest request) {
        return this.additiveServiceImpl.updateById(additive,request);
    }
    


}